package com.trade.okxserver.operator;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.Operator;
import com.trade.okxserver.util.ExpressUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class ScaleOperator extends Operator {

    public ScaleOperator(BigDecimal scale) {
        this.scale = scale;
    }

    private final BigDecimal scale;

    @Override
    public Object executeInner(Object[] objects) throws Exception {
        Object object = objects[0];
        if (object instanceof BigDecimal){
            BigDecimal bigDecimal = (BigDecimal) object;
            return bigDecimal.setScale(scale.scale(), RoundingMode.HALF_UP);
        }
        return object;
    }

    public static void main(String[] args) throws Exception {
        ExpressUtils.getRunner().addFunction("abc", new ScaleOperator(new BigDecimal("0.0001")));
        DefaultContext<String, Object> objectObjectDefaultContext = new DefaultContext<>();
        objectObjectDefaultContext.put("a", new BigDecimal("1"));
        System.out.println(ExpressUtils.execute("abc(1+2)", objectObjectDefaultContext));
    }
}
